//
//  HeadView.m
//  恒记
//
//  Created by Ctair on 16/5/6.
//  Copyright © 2016年 can. All rights reserved.
//

#import "HeadView.h"

@interface HeadView ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_tipsArray;
    NSString *_money;
}
@property (nonatomic, strong) UILabel * dateLabel;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UIImageView * headImageView;
@property (nonatomic, strong) UIView * layerView;
@property (nonatomic, strong) UIView * animationView;
- (void)initializeDataSource;/**< 初始化数据源 */
- (void)initializeUserInterface;/**< 初始化用户界面 */
@end

@implementation HeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeDataSource];
        [self initializeUserInterface];
    }
    return self;
}
- (void)initializeDataSource{
    _tipsArray = [NSArray arrayWithObjects:@"本月预算", @"本月支出", @"预算余额", nil];
}
- (void)initializeUserInterface{
    [self addSubview:self.headImageView];
    [self addSubview:self.dateLabel];
    [self addSubview:self.tableView];
    [self addSubview:self.layerView];
    [self.layerView addSubview:self.animationView];
    [self animation];
}
#pragma mark *** UITableViewDataSource ***
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tipsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"kCell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = _tipsArray[indexPath.row];
    cell.detailTextLabel.text = @"00.00";
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@.00", _money];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark *** UITableViewDelegate ***
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请设置本月预计支出" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UITextField *textfield = alertViewController.textFields.firstObject;
            _money = textfield.text;
//            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
//            });
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alertViewController addTextFieldWithConfigurationHandler:nil];
        [alertViewController addAction:action];
        [alertViewController addAction:cancel];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertViewController animated:YES completion:nil];
        
    }
    NSLog(@"%ld", indexPath.row);
}
- (void)animation{
    [UIView animateWithDuration:2 animations:^{
        self.animationView.frame = CGRectMake(3, CGRectGetHeight(self.layerView.frame) - 3, CGRectGetWidth(self.layerView.frame) - 6, - CGRectGetHeight(self.layerView.frame) + 6);
        
        
    }completion:^(BOOL finished) {

        [UIView animateWithDuration:2 animations:^{
            self.animationView.frame = CGRectMake(3, CGRectGetHeight(self.layerView.frame) - 3, CGRectGetWidth(self.layerView.frame) - 6, 100 * PROPORTION_H - CGRectGetHeight(self.layerView.frame));
            _animationView.backgroundColor = [UIColor redColor];
        }];
    }];
}

#pragma mark *** Getter ***
- (UIView *)animationView{
    if (_animationView == nil) {
        _animationView = [[UIView alloc] init];
        _animationView.frame = CGRectMake(3, CGRectGetHeight(self.layerView.frame) - 3, CGRectGetWidth(self.layerView.frame) - 6, 0);
        _animationView.backgroundColor = [UIColor greenColor];
    }
    return _animationView;
}
- (UIView *)layerView{
    if (_layerView == nil) {
        _layerView = [[UIView alloc] init];
        _layerView.frame = CGRectMake(CGRectGetMaxX(self.tableView.frame) + 30 * PROPORTION_W, CGRectGetMinY(self.tableView.frame) + 30 * PROPORTION_W, SCREEN_WIDTH - CGRectGetWidth(self.tableView.frame) - 60 * PROPORTION_W, CGRectGetHeight(self.tableView.frame) - 50 * PROPORTION_H);
        _layerView.layer.borderColor = [UIColor colorWithRed:0.633 green:0.317 blue:0.000 alpha:1.000].CGColor;
        _layerView.layer.borderWidth = 3;
        _layerView.backgroundColor = [UIColor colorWithRed:0.845 green:0.423 blue:0.000 alpha:1.000];
    }
    return _layerView;
}
- (UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithFrame:self.frame];
        [_headImageView setImage:[UIImage imageNamed:@"back5"]];
        _headImageView.alpha = 0.3;
        
    }
    return _headImageView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CRM(0, 70 * PROPORTION_H, SCREEN_WIDTH - 130 * PROPORTION_W, CGRectGetHeight(self.bounds) - 100 * PROPORTION_H)];
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = CGRectGetHeight(self.tableView.frame)/3;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CRM(20 * PROPORTION_W, 30 * PROPORTION_H, MID_X, 20 * PROPORTION_H)];
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.font = [UIFont systemFontOfSize:20];
        _dateLabel.text = @"5/2016";
    }
    return _dateLabel;
}
@end
